package team.niit.bean;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.crypto.SecureUtil;
import lombok.Data;
import org.osgi.application.ApplicationContext;
import team.niit.entity.AdminEntity;
import team.niit.entity.StudentEntity;
import team.niit.service.AdminService;
import team.niit.service.StudentService;
import team.niit.service.impl.AdminServiceImpl;
import team.niit.service.impl.StudentServiceImpl;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.SessionScoped;
import java.util.List;

/**
 * @Author: 然然然儿呐_
 * @PackageName team.niit.bean
 * @Description: TO DO
 * @Date 2021/12/6 15:45
 */

@Data
@ManagedBean(name = "admin")
@SessionScoped
public class AdminBean {
    /**
     * 学生编号
     */
    private String studentId;
    /**
     * 学生姓名
     */
    private String studentName;
    /**
     * 学生性别
     */
    private String studentGender;
    /**
     * 学生生日
     */
    private String studentBirth;
    /**
     * 学生学院
     */
    private String studentSchool;
    /**
     * 学生专业
     */
    private String studentMajor;
    /**
     * 学生班级
     */
    private String studentClass;
    /**
     * 学生密码
     */
    private String studentPassword;
    /**
     * 学生电话
     */
    private String studentPhone;

    /**
     * 所有学生
     */
    public List<StudentEntity> studentList;

    /**
     * 分页查询的页码
     */
    @Deprecated
    public int pageNumber = 0;

    /**
     * 所有课程的页数
     */
    @Deprecated
    public int totalPage = 0;

    /**
     * 每页大小
     */
    @Deprecated
    public int size = 10;

    /**
     * 所有学生数量
     */
    @Deprecated
    public int count = 0;


    /**
     * service
     */
    StudentService studentService = new StudentServiceImpl();
    AdminService adminService = new AdminServiceImpl();

    /**
     * 页面初始化时执行，用于初始化学生集合信息，推荐使用paging()方法进行初始化
     */
    @Deprecated
    public void init() {
        paging("first");
    }


    /**
     * 查看学生列表
     */
    public List<StudentEntity> selectStudentList() {
        // this.count = this.studentList.size();
        return this.studentList = studentService.selectStudentList();
    }

    public String addStudent() {
        StudentEntity student = new StudentEntity();
        student.setId(this.studentId);
        student.setName(this.studentName);
        student.setGender(this.studentGender);
        student.setBirth(this.studentBirth);
        student.setSchool(this.studentSchool);
        student.setMajor(this.studentMajor);
        student.setClasses(this.studentClass);
        student.setPhone(this.studentPhone);
        student.setPassword(SecureUtil.md5("123456"));
        studentService.addStudent(student);
        return "admin_view_all_student";
    }

    /**
     * 删除学生
     *
     * @param studentId 学生编号
     * @return 返回成功与否的提示信息
     */
    public void dropStudent(String studentId) {
        studentService.dropStudent(studentId);
        selectStudentList();
    }

    /**
     * 分页查询，根据前端传入的按钮类型，进行相应的查询操作
     *
     * @param type 前端点击的按钮类型
     */
    @Deprecated
    public void paging(String type) {
        this.count = studentService.count();
        totalPage = (int) Math.ceil((double) this.count / this.size);
        switch (type) {
            case "first":
                this.pageNumber = 0;
                break;
            case "previous":
                if (this.pageNumber > 0) {
                    this.pageNumber--;
                }
                break;
            case "next":
                if (this.pageNumber < this.totalPage - 1) {
                    this.pageNumber++;
                }
                break;
            case "last":
                this.pageNumber = totalPage - 1;
                break;
        }
        this.studentList = studentService.pagingQuery(this.size, this.pageNumber);
    }

    /**
     * 管理员修改个人信息
     *
     * @return
     */
    public String modifyAdminById(Object o) {
        AdminEntity adminEntity = new AdminEntity();
        BeanUtil.copyProperties(o, adminEntity);
        adminService.modifyAdminById(adminEntity);
        return "admin_view_info";
    }

}
